from waveapi import events
from waveapi import robot
from waveapi import element
from waveapi import ops
from waveapi import appengine_robot_runner

import logging
from xml.sax import saxutils

def OnRobotAdded(event, wavelet):
  """Invoked when the robot has been added."""
  wave_id = wavelet.wave_id
  wave_title = wavelet.title
  blip = event.blip
  blip_id = blip.blip_id
  gadget_url = 'http://embed-bot.appspot.com/gadgets/gadget.xml'
  gadget = element.Gadget(gadget_url, {
                          'waveId':wave_id,
                          'waveTitle':wave_title,
                          'blipId':blip_id
                          })
  wavelet.root_blip.reply().append(gadget)

if __name__ == '__main__':
  embedBot = robot.Robot('Embed Bot', 
      image_url='http://embed-bot.appspot.com/images/icon.png',
      profile_url='http://embed-bot.appspot.com/about')
  embedBot.register_handler(events.WaveletSelfAdded, OnRobotAdded)
  appengine_robot_runner.run(embedBot) 